//
//  JQFYCreatGroupFlockInfoTableViewCell.m
//  JQFY
//
//  Created by jack on 2017/10/9.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYCreatGroupFlockInfoTableViewCell.h"
#import "MBProgressHUD+ZH.h"

static CGFloat JQFYCreatGroupFlockInfoTableViewCellConstraintsHeight = 30;
static CGFloat JQFYCreatGroupFlockInfoTableViewCellConstraintsWidth = 80;
static NSInteger JQFYCreatGroupFlockInfoTableViewCellButtonBase = 983;

@interface JQFYCreatGroupFlockInfoTableViewCell ()

@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) NSArray<UIButton *> *constraints;
@property (nonatomic, strong) UIImageView *flockImageView;
@property (nonatomic, assign) NSInteger selected;

@end

@implementation JQFYCreatGroupFlockInfoTableViewCell

- (void)tapAction:(UIButton *)button {
    if ((button.tag - JQFYCreatGroupFlockInfoTableViewCellButtonBase) == _creatGroupModel.selectedIndex) {
        return;
    }
    UIButton *old = _constraints[_creatGroupModel.selectedIndex];
    old.selected = NO;
    _creatGroupModel.selectedIndex = button.tag - JQFYCreatGroupFlockInfoTableViewCellButtonBase;
    UIButton *new = _constraints[_creatGroupModel.selectedIndex];
    new.selected = YES;
}

#pragma mark - setter/getter

- (void)setCreatGroupModel:(JQFYCreatGroupModel *)creatGroupModel {
    if (_creatGroupModel == creatGroupModel) {
        return;
    }
    _creatGroupModel = creatGroupModel;
    _addressLabel.text = creatGroupModel.address;
    [_constraints enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = (idx == creatGroupModel.selectedIndex);
        JQFYImageModel *imageModel = creatGroupModel.constraints[idx];
        [obj setImage:[UIImage imageNamed:imageModel.image] forState:UIControlStateNormal];
        [obj setImage:[UIImage imageNamed:imageModel.selectImage] forState:UIControlStateSelected];
        obj.tag = JQFYCreatGroupFlockInfoTableViewCellButtonBase + idx;
    }];
    [_flockImageView setImage:[UIImage imageNamed:creatGroupModel.flockInfo.icon.image]];
}


#pragma mark - override

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:15 *SCAL];
    _addressLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_addressLabel];
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(10 *SCAL);
        make.trailing.equalTo(self.contentView).offset(-10 *SCAL);
        make.top.equalTo(self.contentView).offset(10 *SCAL);
    }];
    
    UIView *buttonViews = [[UIView alloc] init];
    [self.contentView addSubview:buttonViews];
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [buttonViews addSubview:button];
        [mutableArray addObject:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo((JQFYCreatGroupFlockInfoTableViewCellConstraintsWidth + 15 *SCAL) * i);
            make.width.mas_equalTo(JQFYCreatGroupFlockInfoTableViewCellConstraintsWidth);
            make.top.bottom.equalTo(buttonViews);
        }];
    }
    _constraints = mutableArray.copy;
    
    [buttonViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(_addressLabel.mas_bottom).offset(10 *SCAL);
        make.width.mas_equalTo(JQFYCreatGroupFlockInfoTableViewCellConstraintsWidth * 3 + 15 *SCAL * 2);
        make.height.mas_equalTo(JQFYCreatGroupFlockInfoTableViewCellConstraintsHeight);
    }];
    
    _flockImageView = [[UIImageView alloc] init];
    _flockImageView.layer.cornerRadius = 14.5 *SCAL;
    _flockImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:_flockImageView];
    
    [_flockImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(buttonViews.mas_bottom).offset(9 *SCAL);
        make.size.mas_equalTo(58 *SCAL);
    }];
    
    UIImageView *flockNameImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"creatGroup_flockIcon"]];
    [self.contentView addSubview:flockNameImageView];
    [flockNameImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(_flockImageView.mas_bottom).offset(9 *SCAL);
        make.width.mas_equalTo(25 *SCAL);
        make.height.mas_equalTo(15 *SCAL);
    }];
    
    UIButton *creatGroupButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [creatGroupButton setBackgroundImage:[UIImage imageNamed:@"creatGroup_sureBtn"] forState:UIControlStateNormal];
    [creatGroupButton addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
//    [creatGroupButton setTitle:@"确认创建" forState:UIControlStateNormal];
    [self.contentView addSubview:creatGroupButton];
    
    [creatGroupButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(35 *SCAL);
        make.trailing.equalTo(self.contentView).offset(-35 *SCAL);
        make.top.equalTo(flockNameImageView.mas_bottom).offset(10 *SCAL);
        make.height.mas_equalTo(35 *SCAL);
    }];
}

- (void)tapAction {
//    NSLog(@"点击了确认创建");
    [MBProgressHUD showWithMessage:@"确认创建"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCreatGroup)]) {
        [self.delegate didCreatGroup];
    }
}

+ (CGFloat)heigtForCell {
    CGFloat height = 0;
    height += 10 *SCAL;
    height += 15 *SCAL;
    height += 11 *SCAL;
    height += JQFYCreatGroupFlockInfoTableViewCellConstraintsHeight;
    height += 9 *SCAL;
    height += 29 *SCAL;
    height += 9 *SCAL;
    height += 15 *SCAL;
    height += 10 *SCAL;
    height += 35 *SCAL;
    height += 15 *SCAL;
    return height;
}

@end
